package com.base.study.activity;

import android.os.NetworkOnMainThreadException;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.JsonWriter;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.sp.SpUtil;
import com.base.study.R;
import com.base.study.adapter.EsDetailAdapter;
import com.base.study.bean.EsDetailBean;
import com.base.study.bean.EsSearchResultBean;
import com.base.study.bean.SubjectListBean;
import com.base.study.databinding.ActivityEsSearchDetailBinding;
import com.base.study.databinding.ActivitySubjectListBinding;
import com.base.study.presenter.ESSearchDetailPresenter;
import com.base.study.presenter.SubjectListPresenter;
import com.base.study.view.ESSearchDetailView;
import com.base.study.widget.RecyclerViewSpacesItemDecoration;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.content.ContentValues.TAG;

@Route(path = "/search/detail")
public class ESSearchDetailActivity extends CBaseBarMvpActivity<ESSearchDetailPresenter> implements ESSearchDetailView {
    private OkHttpClient okHttpClient;
    private ActivityEsSearchDetailBinding mBinding;
    private EsDetailAdapter esDetailAdapter;

    @Autowired
    public String id;
    @Autowired
    public String title;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_es_search_detail;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityEsSearchDetailBinding) mRootBinding;

       // mPresenter = new SubjectListPresenter(this, this);
    }

    @Override
    protected void start() {
        setBackNavigation();
        setBarTitle(title);
        //mPresenter =new ESSearchDetailPresenter(this,this);
        //mPresenter.getData("");



       // mBinding.recycler.setAdapter(subjectListBeanBaseAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.recycler.setLayoutManager(linearLayoutManager);
        mBinding.recycler.addItemDecoration(new RecyclerViewSpacesItemDecoration
                (10, 10, 10, 10));
        getData(id);
    }

    @Override
    public void returnNoData() {

    }

    @Override
    public void returnData(EsSearchResultBean data) {
    }

    @Override
    public void returnError() {

    }


    private void getData(String id){
            if (okHttpClient == null){
                okHttpClient = new OkHttpClient();
            }
            String url = "http://192.168.1.31:8989/api-legal/legal/getOriginalfile?flfgId="+id;
            //Log.e("上传地址:",url);
            Request request = new Request.Builder()
                    .url(url)
                    .addHeader("token", SpUtil.getToken())
                    .addHeader("User-Agent", "ANDROID")
                    .get()//默认就是GET请求，可以不写
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.d(TAG, "onFailure: ");
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    //Log.e(TAG, "onResponse: " + response.body().string());
                    String json = response.body().string();
                    EsDetailBean esDetailBean=new Gson().fromJson(json,EsDetailBean.class);
                    esDetailAdapter=new EsDetailAdapter(mContext,esDetailBean);

                    ESSearchDetailActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBinding.recycler.setAdapter(esDetailAdapter);

                        }
                    });
                }
            });


    }


}
